//
//  GameXmlParser.m
//  xKey Interface
//
//  Created by Guillaume SOURDRILLE on 19/02/12.
//  Copyright (c) 2012 Edenyorke. All rights reserved.
//

#import "GameXmlParser.h"
#import "URLConnection.h"
#import "NSDataAdditions.h"

@implementation GameXmlParser

-(id)  completeXml:(id) donnees {
               
        title = [[NSMutableString alloc]init ];
        description = [[NSMutableString alloc]init ];
        cover = [[NSMutableString alloc]init ];
        banner = [[NSMutableString alloc]init ];
    
        
        URLConnection *urlConnection = [[URLConnection alloc] initWithIp];
        NSString *gameId = [donnees objectForKey:@"ID"];
        NSURL *url = [urlConnection getURLXmlForId:gameId];
        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
        [xmlParser setDelegate:self];
        [xmlParser parse];
        
        NSString *test = [title stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                
        if (([test isEqualToString:@"No Title"] == NO)){ 
        
            if (title  != nil && ([title isEqualToString:@""] == NO)){
                [donnees setObject:[title stringByReplacingOccurrencesOfString:@"\n" withString:@""] forKey:@"TITLE"];
            }   
            
            if (description  != nil && ([description isEqualToString:@""] == NO)){
                [donnees setObject:description forKey:@"DESCRIPTION"];
            }
      
            if (cover  != nil && ([cover isEqualToString:@""] == NO)){
                NSString *cover64 = [cover stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                NSData *dataObj = [NSData dataWithBase64EncodedString:cover64];
                UIImage *image = [UIImage imageWithData:dataObj];
                [donnees setObject:image forKey:@"COVER"];
            }
        
        
            if (banner  != nil && ([banner isEqualToString:@""] == NO)){
                NSString *banner64 = [banner stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                NSData *dataObj = [NSData dataWithBase64EncodedString:banner64];
                UIImage *image = [UIImage imageWithData:dataObj];
                [donnees setObject:image forKey:@"BANNER"];

            }
        }
    
    
      
    
    return donnees;
    
}

-(UIImage*) getCoverFromId: (NSString*) gameId{
    URLConnection *urlConnection = [[URLConnection alloc] initWithIp];
    UIImage *image =  [urlConnection checkImageForId:gameId];
    return image;
    
}


-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
 namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{ 
    currentElement = elementName;
    NSLog(@"%@",elementName);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{  
    
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    
    if ([currentElement isEqualToString:@"title"]) 
    {
        [title appendString:string];
    } else if ([currentElement isEqualToString:@"banner"]) 
    {
        [banner appendString:string];
    }  else if ([currentElement isEqualToString:@"boxart"]) {
        [cover appendString:string];
    } else if  ([currentElement isEqualToString:@"summary"]) {
        [description appendString:string];
    }
}




@end
